namespace CIS.Resource
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ResourceAgenciesPageEdit : Page
    {
        protected TextBox AgenciesAddressTxb;
        protected DropDownList AgenciesCityDdl;
        protected TextBox AgenciesNameTxb;
        protected TextBox AgenciesPhoneTxb;
        protected DropDownList AgenciesProvinceDdl;
        protected Image iResource;
        protected Label lbAgenciesAddress;
        protected Label lbAgenciesCity;
        protected Label lbAgenciesName;
        protected Label lbAgenciesPhone;
        protected Label lbAgenciesProvince;
        protected UserPrincipal Principal = null;
        protected Button SaveBtn;

        private void AgenciesProvinceDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.AgenciesCityDdl.DataSource = ManageCity.GetCityViaProvinceId(new Guid(this.AgenciesProvinceDdl.SelectedItem.Value));
            this.AgenciesCityDdl.DataTextField = "regionName";
            this.AgenciesCityDdl.DataValueField = "regionId";
            this.AgenciesCityDdl.DataBind();
        }

        private void FillPage()
        {
            if (base.Request.QueryString["IsModify"] == "True")
            {
                Guid id = new Guid(base.Request.QueryString["AgenciesId"]);
                ResourceAgencies resourceAgencies = ManageResource.GetResourceAgenciesInfoViaId(id);
                this.AgenciesNameTxb.Text = resourceAgencies.AgenciesName;
                this.AgenciesAddressTxb.Text = resourceAgencies.AgenciesAddress;
                this.AgenciesPhoneTxb.Text = resourceAgencies.AgenciesPhone;
                for (int i = 0; i < this.AgenciesProvinceDdl.Items.Count; i++)
                {
                    if (this.AgenciesProvinceDdl.Items[i].Value == resourceAgencies.AgenciesProvince.ToString())
                    {
                        this.AgenciesProvinceDdl.SelectedIndex = i;
                        break;
                    }
                }
                this.AgenciesCityDdl.DataSource = ManageCity.GetCityViaProvinceId(resourceAgencies.AgenciesProvince);
                this.AgenciesCityDdl.DataTextField = "regionName";
                this.AgenciesCityDdl.DataValueField = "regionId";
                this.AgenciesCityDdl.DataBind();
                bool result = false;
                for (int i = 0; i < this.AgenciesCityDdl.Items.Count; i++)
                {
                    if (this.AgenciesCityDdl.Items[i].Value == resourceAgencies.AgenciesCity.ToString())
                    {
                        this.AgenciesCityDdl.SelectedIndex = i;
                        result = true;
                        break;
                    }
                }
                if (!result)
                {
                    this.AgenciesCityDdl.Items.Clear();
                }
                for (int i = 0; i < this.AgenciesCityDdl.Items.Count; i++)
                {
                    if (this.AgenciesCityDdl.Items[i].Value == resourceAgencies.AgenciesCity.ToString())
                    {
                        this.AgenciesCityDdl.SelectedIndex = i;
                    }
                }
            }
        }

        private void InitializeComponent()
        {
            this.SaveBtn.Click += new EventHandler(this.SaveBtn_Click);
            this.AgenciesProvinceDdl.SelectedIndexChanged += new EventHandler(this.AgenciesProvinceDdl_SelectedIndexChanged);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFromImage();
            this.SetFrom();
            this.AgenciesProvinceDdl.DataSource = ManageProvince.GetProvince();
            this.AgenciesProvinceDdl.DataTextField = "RegionName";
            this.AgenciesProvinceDdl.DataValueField = "RegionId";
            this.AgenciesProvinceDdl.DataBind();
            this.AgenciesProvinceDdl.Items.RemoveAt(0);
            this.AgenciesCityDdl.DataSource = ManageCity.GetCityViaProvinceId(new Guid(this.AgenciesProvinceDdl.SelectedItem.Value));
            this.AgenciesCityDdl.DataTextField = "regionName";
            this.AgenciesCityDdl.DataValueField = "regionId";
            this.AgenciesCityDdl.DataBind();
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            string strAgenciesName = this.AgenciesNameTxb.Text.Trim();
            string strAgenciesAddress = this.AgenciesAddressTxb.Text.Trim();
            string strAgenciesPhone = this.AgenciesPhoneTxb.Text.Trim();
            if (base.Request.QueryString["IsModify"] == "True")
            {
                if (strAgenciesName != "")
                {
                    Guid cityId = new Guid("00000000-0000-0000-0000-000000000001");
                    if (this.AgenciesCityDdl.Items.Count != 0)
                    {
                        cityId = new Guid(this.AgenciesCityDdl.SelectedItem.Value);
                    }
                    ManageResource.UpdateResourceAgenciesInfo(new Guid(base.Request.QueryString["AgenciesId"]), strAgenciesName, new Guid(this.AgenciesProvinceDdl.SelectedItem.Value), cityId, strAgenciesAddress, strAgenciesPhone);
                    base.Response.Redirect("~/CIS/Resource/ResourceAgenciesPage.aspx", true);
                }
                base.Response.Write("<script> alert('Name is Null !') </script>");
            }
            else if (strAgenciesName != "")
            {
                if (!ManageResource.IsExistSameResourceAgenciesInfo(strAgenciesName))
                {
                    Guid cityId = new Guid("00000000-0000-0000-0000-000000000001");
                    if (this.AgenciesCityDdl.Items.Count != 0)
                    {
                        cityId = new Guid(this.AgenciesCityDdl.SelectedItem.Value);
                    }
                    ManageResource.InsertNewResourceAgenciesInfo(strAgenciesName, new Guid(this.AgenciesProvinceDdl.SelectedItem.Value), cityId, strAgenciesAddress, strAgenciesPhone);
                    base.Response.Redirect("~/CIS/Resource/ResourceAgenciesPage.aspx", true);
                }
                else
                {
                    base.Response.Write("<script> alert('该信息已存在') </script>");
                }
            }
        }

        private void SelectAuth()
        {
            if (this.Principal.OwnRole.TypeC == 0)
            {
                this.InitPage();
                this.FillPage();
                this.SaveBtn.Visible = false;
            }
            else if (this.Principal.OwnRole.TypeC == 1)
            {
                this.InitPage();
                this.FillPage();
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            this.lbAgenciesName.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceAgenciesPageEdit", "lbAgenciesName", language, path);
            if (this.Principal.OwnArea[0].AreaId.ToString() == "a7f26634-10a9-4cb1-aa2c-8c91a8dfdd66")
            {
                this.lbAgenciesProvince.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceAgenciesPageEdit", "lbAgenciesProvinceOverSea", language, path);
                this.lbAgenciesCity.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceAgenciesPageEdit", "lbAgenciesCityOverSea", language, path);
            }
            else
            {
                this.lbAgenciesProvince.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceAgenciesPageEdit", "lbAgenciesProvince", language, path);
                this.lbAgenciesCity.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceAgenciesPageEdit", "lbAgenciesCity", language, path);
            }
            this.lbAgenciesAddress.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceAgenciesPageEdit", "lbAgenciesAddress", language, path);
            this.lbAgenciesPhone.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceAgenciesPageEdit", "lbAgenciesPhone", language, path);
            this.SaveBtn.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceAgenciesPageEdit", "SaveBtn", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iResource.ImageUrl = "../images/title_SharedInfo.jpg";
                    break;

                case "en":
                    this.iResource.ImageUrl = "../images/title_SharedInfoEn.jpg";
                    break;
            }
        }
    }
}

